Laravel 提供一個互動式命令列工具 Tinker,可以讓我們直接在命令列中執行 PHP 程式碼,非常適合用來:
php artisan tinker
進入後會出現互動式的命令列環境,可以直接執行 PHP 程式碼。
假設你有一個任務類別叫做 ProcessInvoiceJob,你可以直接在 Tinker 中這樣執行:dispatch(new \App\Jobs\ProcessInvoiceJob($yourPayload));
$yourPayload 可以是你要傳入 Job 的資料,如陣列或模型。
新增一筆資料:
App\Models\File::create([
'title' => 'test',
'content' => 'demo for tinker',
]);
你必須確認 App\Models\File
有對應的 $fillable 欄位允許 title 與 content。
App\Models\File::get();
Tinker 不會自動同步你的程式碼變更,
如果此時 Tinker 還開著,請先退出再重開,才會載入最新的程式碼。
exit # 離開 Tinker
php artisan tinker # 重新開啟